package com.xuecheng.content.controller;

import com.xuecheng.api.content.TeachplanApi;
import com.xuecheng.api.content.model.dto.TeachplanDTO;
import com.xuecheng.api.content.model.dto.TeachplanMediaDTO;
import com.xuecheng.api.content.model.vo.BindTeachplanMediaVO;
import com.xuecheng.api.content.model.vo.TeachplanVO;
import com.xuecheng.api.teaching.model.dto.TeachplanWorkDTO;
import com.xuecheng.api.teaching.model.vo.BindTeachplanWorkVO;
import com.xuecheng.common.domain.response.RestResponse;
import com.xuecheng.content.common.util.UAASecurityUtil;
import com.xuecheng.content.convert.TeachplanConvert;
import com.xuecheng.content.service.TeachplanMediaService;
import com.xuecheng.content.service.TeachplanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程计划 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class TeachplanController implements TeachplanApi {

    @Autowired
    private TeachplanService  teachplanService;

    @GetMapping("teachplan/{courseId}/tree-nodes")
    public TeachplanDTO getTreeNodes(@PathVariable Long courseId) {

        Long companyId = UAASecurityUtil.getCompanyId();

        TeachplanDTO dto = teachplanService.getTreeNodes(courseId, companyId);

        return dto;
    }

    @PostMapping("teachplan")
    public TeachplanDTO createOrModifyTeachplan(@RequestBody TeachplanVO vo) {

        Long companyId = UAASecurityUtil.getCompanyId();

        TeachplanDTO dto = TeachplanConvert.INSTANCE.vo2dto(vo);

        TeachplanDTO resultDTO = teachplanService.createOrModifyTeachplan(dto, companyId);

        return resultDTO;
    }

    @DeleteMapping("teachplan/{teachplanId}")
    public void removeTeachPlan(@PathVariable Long teachplanId) {

        // 1.获得公司Id
        Long companyId = UAASecurityUtil.getCompanyId();

        // 2.调用service方法
        teachplanService.removeTeachPlan(teachplanId,companyId);
    }

    @PostMapping("teachplan/media/association")
    public TeachplanMediaDTO associationMedia(@RequestBody BindTeachplanMediaVO mediaVO) {

        Long companyId = UAASecurityUtil.getCompanyId();

        TeachplanMediaDTO teachplanMediaDTO = new TeachplanMediaDTO();
        teachplanMediaDTO.setTeachplanId(mediaVO.getTeachplanId());
        teachplanMediaDTO.setMediaId(mediaVO.getMediaId());

        TeachplanMediaDTO resultDTO = teachplanService.associationMedia(teachplanMediaDTO, companyId);

        return resultDTO;
    }

    @Autowired
    private TeachplanMediaService teachplanMediaService;

    @GetMapping("l/teachplan/media/{mediaId}")
    public RestResponse<List<TeachplanMediaDTO>> getByMediaId4s(@PathVariable Long mediaId) {
        return teachplanMediaService.getByMediaId4s(mediaId);
    }

    /**
     * 课程计划和作业信息绑定
     * @param vo
     * @return
     */
    @PostMapping("teachplan/work/association")
    public TeachplanWorkDTO associateWork(@RequestBody BindTeachplanWorkVO vo) {
        //公司id
        Long companyId = UAASecurityUtil.getCompanyId();
        //vo转dto
        TeachplanWorkDTO dto = new TeachplanWorkDTO();
        dto.setTeachplanId(vo.getTeachplanId());
        dto.setWorkId(vo.getWorkId());
        dto.setWorkTitle(vo.getWorkTitle());
        //调用Service
        TeachplanWorkDTO resultDTO = teachplanService.associateWork(dto, companyId);
        return resultDTO;
    }

    /**
     * 课程计划和作业信息解绑
     * @param teachplanWorkId
     */
    @DeleteMapping("/teachplan/work/{teachplanWorkId}")
    public void deleteTeachplanWork(@PathVariable String teachplanWorkId,Long courseBaseId) {
        //公司id
        Long companyId = UAASecurityUtil.getCompanyId();
        teachplanService.deleteTeachplanWork(teachplanWorkId,courseBaseId,companyId);
    }


}
